#!/usr/bin/python
#coding:utf8
"""
Strategy
In most of other languages Strategy pattern is implemented via creating some base strategy interface/abstract class and
subclassing it with a number of concrete strategies (as we can see at http://en.wikipedia.org/wiki/Strategy_pattern),
however Python supports higher-order functions and allows us to have only one class and inject functions into it's
instances, as shown in this example.
"""
import types

class PaymentStrategy:
    def __init__(self, payFun=None):
        self.name = "sqy"
        if payFun is not None:
            self.payMoney = types.MethodType(payFun, self)
    
    def payMoney(self, amount):
        print(f"{self.name}默认采用支付宝支付：{amount}")

def creditCard(self,amount):
    print(f"{self.name}采用信用卡支付：{amount}")

def weiXin(self,amount):
    print(f"{self.name}采用微信支付：{amount}")

if __name__ == '__main__':
    pay1 = PaymentStrategy()
    pay2 = PaymentStrategy(creditCard)
    pay3 = PaymentStrategy(weiXin)
    pay1.payMoney(100)
    pay2.payMoney(200)
    pay3.payMoney(300)
